package org.niux.AutoLoginQQ.reg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

import org.niux.AutoLoginQQ.util.EncodeQQPass;

/**
 * 
 * Copyright (c) 2012,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：Reg.java<br>
 * 摘 要：注册模块<br>
 * <br>
 * 当前版本：<br>
 * 作 者：mingxian Email: <a
 * href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2012-10-26<br>
 * <br>
 * 取代版本：<br>
 * 原作者 ：<br>
 * 完成日期：@since 2012-10-26<br>
 */
public class Reg {

	/**
	 * 
	 * 方法概述：获取机器码 描述：获取机器码
	 * 
	 * @return 返回类型：String 修改人：梁铭显 修改时间：2012-10-26
	 */
	public String getGUID() {
		String disk = EncodeQQPass.md5(getDiskSerialNumber()).substring(8,16);
		String mainBoard = EncodeQQPass.md5(getMainBoardSerialNumber()).substring(8,16);
		String mac = EncodeQQPass.md5(getMac()).substring(8,16);
		
		String identify = disk + "-" + mainBoard + "-" + mac;
		
		return identify;
	}
	
	/**
	 * 
	 * 方法概述：获取主板序列号
	 * 描述：获取主板序列号
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-10-31
	 */
	public String getMainBoardSerialNumber(){
		String serialNumber = "";
		try {
			File file = File.createTempFile("mainBoard", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "MsgValue = \"\"\n"
					+ "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set ColItems = objWMIServiCe.ExeCQuery(\"SeleCt * from Win32_BaseBoard\", , 48)\n"
					+ "For Each objItem In colItems\n"
					+ "MsgValue = objItem.SerialNumber\n"
					+ "Next\n"
					+ "WsCript.ECho MsgValue"; // see note
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				serialNumber += line;
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return serialNumber;
	}
	/**
	 * 
	 * 方法概述：获取硬盘序列号
	 * 描述：获取硬盘序列号
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-10-31
	 */
	public String getDiskSerialNumber(){
		String serialNumber = "";
		try {
			File file = File.createTempFile("disk", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "MsgValue = \"\"\n"
					+ "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set ColItems = objWMIServiCe.ExeCQuery(\"SELECT * FROM Win32_PhysiCalMedia\", , 48)\n"
					+ "For Each objItem In ColItems\n"
					+ "MsgValue = MsgValue & objItem.SerialNumber\n"
					+ "Next\n"
					+ "WsCript.ECho MsgValue"; // see note
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				serialNumber += line;
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return serialNumber.trim();
	}
	/**
	 * 
	 * 方法概述：获取网卡地址
	 * 描述：获取网卡地址
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2012-10-31
	 */
	public String getMac(){
		String mac = "";
		try {
			File file = File.createTempFile("mac", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Dim mc,mo\n"
					+ "Set mc=GetObject(\"Winmgmts:\").InstancesOf(\"Win32_NetworkAdapterConfiguration\")\n"
					+ "For Each mo In mc\n"
					+ "If mo.IPEnabled=True Then\n"
					+ "Wscript.Echo mo.MacAddress\n"
					+ "Exit For\n"
					+ "End If\n"
					+ "Next\n";
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				mac += line;
			}
			input.close();
		} catch (Exception e){
			e.printStackTrace();
		}
		return mac;
	}

	public static void main(String[] args) {
		Reg reg = new Reg();
		System.out.println(reg.getGUID());
	}
}
